//
//  UIView+ZXEmptyView.m
//  ZhiXingUser
//
//  Created by Cocoa on 2019/5/9.
//  Copyright © 2019年 QiaoLi. All rights reserved.
//

#import "UIView+ZXEmptyView.h"

@implementation UIView (ZXEmptyView)

- (void)zx_showNoDataEmptyViewWithBtnAction:(LYActionTapBlock)btnAction {
    @weakify(self);
    
    if ([self isKindOfClass:UIScrollView.class] == YES) {
        BOOL scrollEnabled = [(UIScrollView *)self isScrollEnabled];
        [self zx_setScrollViewEnabled:scrollEnabled];
        [(UIScrollView *)self setScrollEnabled:NO];
    }
    self.ly_emptyView = [ZXEmptyView noDataEmptyWithBtnClickBlock:^{
        if (btnAction) {
            btnAction();
            @strongify(self);
            [self zx_hideEmptyView];
        }
    }];
    self.ly_emptyView.emptyViewIsCompleteCoverSuperView = YES;
    [self ly_showEmptyView];
}

- (void)zx_showNetworkErrorEmptyViewWithBtnAction:(LYActionTapBlock)btnAction {
    @weakify(self);
    self.ly_emptyView = [ZXEmptyView networkErrorEmptyWithBtnClickBlock:^{
        if (btnAction) {
            btnAction();
            @strongify(self);
            [self zx_hideEmptyView];
        }
    }];
    self.ly_emptyView.emptyViewIsCompleteCoverSuperView = YES;
    [self ly_showEmptyView];
}
- (void)zx_showUnLoginErrorForOrderWithBtnAction:(LYActionTapBlock)btnAction {
//    @weakify(self);
    self.ly_emptyView = [ZXEmptyView unLoginErrorForOrder:^{
        if (btnAction) {
            btnAction();
//            @strongify(self);
//            [self zx_hideEmptyView];
        }
    }];
    self.ly_emptyView.emptyViewIsCompleteCoverSuperView = YES;
    [self ly_showEmptyView];
}

- (void)zx_hideEmptyView {
    if ([self isKindOfClass:UIScrollView.class] == YES) {
        BOOL scrollEnabled = [self zx_scrollViewEnabled];
        [(UIScrollView *)self setScrollEnabled:scrollEnabled];

    }
    [self ly_hideEmptyView];
    [self.ly_emptyView removeFromSuperview];
    self.ly_emptyView = nil;
}

static NSString* const zx_scrollViewEnable_key = @"zx_scrollViewEnable_key";

- (void)zx_setScrollViewEnabled:(BOOL)enabled {
    objc_setAssociatedObject(self, &zx_scrollViewEnable_key, @(enabled), OBJC_ASSOCIATION_ASSIGN);
}
- (BOOL)zx_scrollViewEnabled {
    return [objc_getAssociatedObject(self, &zx_scrollViewEnable_key) boolValue];
}

@end
